﻿using System;

namespace SkeinLib
{       
    public static partial class ThreeFish
    {
        /// <summary>
        /// Transform eight 64-bit words according to ThreeFish algorithm.
        /// </summary>
        /// <param name="input">The eight 64-bit words to be transformed.</param>
        /// <param name="key">The eight 64-bit key words used during the transformation.</param>
        /// <param name="tweak">The two 64-bit tweak words used in conjunction with the key.</param>
        /// <param name="feed">False for encryption, true for hashing mechanism used by Skein.</param>
        /// <param name="output">The buffer to contain the result of the transform.</param>
        unsafe internal static void ThreeFish512Internal(
                    UInt64[] input, UInt64[] key, UInt64[] tweak, System.Boolean feed, ref UInt64[] output)
        {
            //Error checking REMOVED!
            //If errors or strange behaviors occur, change your references from ThreeFishXXXInternal to ThreeFishXXXWords or
            //uncomment the error checking routines below.
            /*if (input == null)
                throw new System.ArgumentNullException("input", "input cannot be null!");
            if (key == null)
                throw new System.ArgumentNullException("key", "key cannot be null!");
            if (tweak == null)
                throw new System.ArgumentNullException("tweak", "tweak cannot be null!");
            if (output == null)
                throw new System.ArgumentNullException("output", "output cannot be null, initialize the array first!");
            if (input.Length != 8)
                throw new System.ArgumentException("Incorrect length for input word array (length != 8).", "input");
            if (key.Length != 8)
                throw new System.ArgumentException("Incorrect length for key word array (length != 8).", "key");
            if (tweak.Length != 2)
                throw new System.ArgumentException("Incorrect length for tweak word array (length != 2).", "tweak");
            if (output.Length != 8)
                throw new System.ArgumentException("Incorrect length for output word array (length != 8).", "output");
            */
            fixed (UInt64* pInput = input, pKey = key, pTweak = tweak, pResult = output)
            {
                SkeinLib.ThreeFishCore.Process512(pInput, pKey, pTweak, feed, pResult);
            }
            return;
        }

        /// <summary>
        /// If the ThreeFish512Words transformation was done with "feed" = false, the words can be "untransformed" with the
        /// SAME key AND tweak using this function.
        /// </summary>
        /// <param name="input">The eight 64-bit key words to be recovered.</param>
        /// <param name="key">The eight 64-bit key words used during the transformation.</param>
        /// <param name="tweak">The two 64-bit tweak words used in conjunction with the key.</param>
        /// <param name="output">The buffer to contain the result of the transform.</param>
        unsafe internal static void InvThreeFish512Internal(
                    UInt64[] input, UInt64[] key, UInt64[] tweak, ref UInt64[] output)
        {   
            //Error checking REMOVED!
            //If errors or strange behaviors occur, change your references from ThreeFishXXXInternal to ThreeFishXXXWords or
            //uncomment the error checking routines below.
            /*if (input == null)
                throw new System.ArgumentNullException("input", "input cannot be null!");
            if (key == null)
                throw new System.ArgumentNullException("key", "key cannot be null!");
            if (tweak == null)
                throw new System.ArgumentNullException("tweak", "tweak cannot be null!");
            if (output == null)
                throw new System.ArgumentNullException("output", "output cannot be null, initialize the array first!");
            if (input.Length != 8)
                throw new System.ArgumentException("Incorrect length for input word array (length != 8).", "input");
            if (key.Length != 8)
                throw new System.ArgumentException("Incorrect length for key word array (length != 8).", "key");
            if (tweak.Length != 2)
                throw new System.ArgumentException("Incorrect length for tweak word array (length != 2).", "tweak");
            if (output.Length != 8)
                throw new System.ArgumentException("Incorrect length for output word array (length != 8).", "output");
            */
            fixed (UInt64* pInput = input, pKey = key, pTweak = tweak, pResult = output)
            {
                SkeinLib.ThreeFishCore.InvProcess512(pInput, pKey, pTweak, pResult);
            }
            return;            
        }
    }
}
